package com.vpx.server.mapper.system;

import com.vpx.server.bean.entity.SysOperateLog;
import com.vpx.server.bean.order.system.SysOperateLogPageOrder;
import com.vpx.server.framework.base.BaseMapper;
import com.vpx.server.framework.utils.SqlUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.SelectProvider;

import java.util.List;

/**
 * @author liuks
 * @title: vpx
 * @module:
 * @description:
 * @date 2021/09/13 10:04
 */
public interface SysOperateLogMapper extends BaseMapper<SysOperateLog> {

    /**
     * 分页查询操作日志
     * @param order
     * @return
     */
    @SelectProvider(type = SysOperateLogMapper.SqlProvider.class, method = "pageQuery")
    List<SysOperateLog> pageQuery(@Param("order") SysOperateLogPageOrder order);

    class SqlProvider{
        public String pageQuery(@Param("order") SysOperateLogPageOrder order){
            StringBuffer sb = new StringBuffer("select * from sys_operate_log");
            Boolean where = false;
            if (StringUtils.isNotBlank(order.getLoginAccount())) {
                where = SqlUtils.appWhere(where, sb, "login_account like ('%'+#{order.loginAccount}+'%')");
            }
            if (StringUtils.isNotBlank(order.getUserName())) {
                where = SqlUtils.appWhere(where, sb, "user_name like ('%'+#{order.userName}+'%')");
            }
            if (StringUtils.isNotBlank(order.getOperateName())) {
                where = SqlUtils.appWhere(where, sb, "operate_name like ('%'+#{order.operateName}+'%')");
            }
            if (StringUtils.isNotBlank(order.getOperateDateStart())) {
                where = SqlUtils.appWhere(where, sb, " create_time >= #{order.operateDateStart}");
            }
            if (StringUtils.isNotBlank(order.getOperateDateEnd())) {
                where = SqlUtils.appWhere(where, sb, " create_time<= #{order.operateDateEnd}");
            }
//            if (StringUtils.isNotBlank(order.getOperateDateStart())) {
//                where = SqlUtils.appWhere(where, sb, "#{order.operateDateStart} <= date_format(create_time,'%Y-%m-%d')");
//            }
//            if (StringUtils.isNotBlank(order.getOperateDateEnd())) {
//                where = SqlUtils.appWhere(where, sb, "#{order.operateDateEnd} >= date_format(create_time,'%Y-%m-%d')");
//            }
            sb.append(" order by create_time desc");
            return sb.toString();
        }
    }

    @Select("select distinct log_type from sys_operate_log")
    List<String> typeOnSelect();

}
